<?php

namespace app\jigou\controller\qingdongams;

use addons\qingdongams\model\Customer;
use addons\qingdongams\model\Staff;
use app\common\controller\Backend;

/**
 * 分级设置
 *
 * @icon fa fa-circle-o
 */
class Jibie extends Backend
{

    /**
     * Jibie模型对象
     * @var \app\admin\model\qingdongams\Jibie
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\qingdongams\Jibie;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['jigou'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('jigou')->visible(['jigou_name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    public function getcustomernum()
    {
        $qianzai_tian = input("qianzai_tian/d", 0);
        $putong_tian = input("putong_tian/d", 0);
        $xiaoke_tian = input("xiaoke_tian/d", 0);
        $gao_jine = input("gao_jine/d", 0);

        $putong_tian = time() - $putong_tian * 24 * 3600;
        $xiaoke_tian = time() - $xiaoke_tian * 24 * 3600;

        $staff = Staff::info();
        $staff_id = $staff->id;
        $wheres['owner_staff_id'] = array('in', Staff::getMyStaffIds());
        $owner_staff_id = $wheres['owner_staff_id'];

        $qianzai = Customer::where($wheres)->where("createtime", ">=", time() - $qianzai_tian * 24 * 3600)->where("customer_type", "家长")->where(function ($query) use ($staff_id, $owner_staff_id) {
                        $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
                            ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
                            ->whereOr(['owner_staff_id' => $owner_staff_id]);
                })
            ->where("customer_type", "家长")
            ->where('owner_staff_id != 0 and owner_staff_id is not null')->whereRaw("id not in(select customer_id from fa_qingdongams_orderceshi)")->column("id");

        $xin = Customer::where($wheres)->where("customer_type", "家长")->whereRaw("id in(select customer_id from fa_qingdongams_orderceshi where createtime> " . $putong_tian . ")")->column("id");

        $yiban = Customer::where($wheres)->where("createtime", ">=", time() - $xiaoke_tian * 24 * 3600)->where("customer_type", "家长")->where(function ($query) use ($staff_id, $owner_staff_id) {
            $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
                ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
                ->whereOr(['owner_staff_id' => $owner_staff_id]);
        })
            ->where("customer_type", "家长")
            ->where('owner_staff_id != 0 and owner_staff_id is not null')->whereRaw("id in(select customer_id from fa_qingdongams_orderceshi)")->column("id");

        $chenshui = Customer::where($wheres)->where("createtime", ">=", time() - $xiaoke_tian * 24 * 3600)->where("customer_type", "家长")->where(function ($query) use ($staff_id, $owner_staff_id) {
            $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
                ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
                ->whereOr(['owner_staff_id' => $owner_staff_id]);
        })
            ->where("customer_type", "家长")
            ->where('owner_staff_id != 0 and owner_staff_id is not null')->whereRaw("id not in(select customer_id from fa_qingdongams_orderceshi)")->column("id");


        $zhongyao = Customer::where($wheres)->where("createtime", ">=", time() - $xiaoke_tian * 24 * 3600)->where("customer_type", "家长")->where(function ($query) use ($staff_id, $owner_staff_id) {
            $query->where(['ro_staff_id' => ['like', "%,{$staff_id},%"]])
                ->whereOr('rw_staff_id', 'like', "%,{$staff_id},%")
                ->whereOr(['owner_staff_id' => $owner_staff_id]);
        })
            ->where("customer_type", "家长")
            ->where('owner_staff_id != 0 and owner_staff_id is not null')->whereRaw("id not in(select customer_id from fa_qingdongams_orderceshi)")
            ->whereRaw(
                "id IN (SELECT customer_id FROM fa_qingdongams_orderceshi GROUP BY customer_id HAVING SUM(price) > $gao_jine)"
            )
            ->column("id");





        $array = [];
        $array["zhongyao"] = count($zhongyao);
        $array["yiban"] = count($yiban);
        $array["xin"] = count($xin);
        $array["qianzai"] = count($qianzai);
        $array["chenshui"] = count($chenshui);

        $this->success("修改成功", "", $array);
    }

}
